import { createStore } from 'vuex';

interface State {
    searchList: string[];
}

const store = createStore<State>({
    state: {
        searchList: JSON.parse(localStorage.getItem('searchList') || '[]'),
    },
    mutations: {
        add(state, payload: string) {
            const copy = [...state.searchList];
            copy.unshift(payload);
            state.searchList = copy;
            localStorage.setItem('searchList', JSON.stringify(copy));
        },
        clear(state) {
            state.searchList = [];
            localStorage.setItem('searchList', JSON.stringify([]));
        },
    },
    actions: {
        add({ commit }, payload: string) {
            commit('add', payload);
        },
        clear({ commit }) {
            commit('clear');
        },
    },
    getters: {
        searchList: (state) => state.searchList,
    },
});

export default store;